package com.jiamao.goplay.ui.user;

import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.text.InputFilter;
import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProviders;

import com.discussionavatarview.DensityUtil;
import com.gyf.immersionbar.ImmersionBar;
import com.jiamao.goplay.BR;
import com.jiamao.goplay.R;
import com.jiamao.goplay.app.AppViewModelFactory;
import com.jiamao.goplay.base.BaseMVVMFragment;
import com.jiamao.goplay.config.Config;
import com.jiamao.goplay.databinding.UserUpdateNameLayoutBinding;
import com.jiamao.goplay.ui.user.vm.UpdateInfoVM;
import com.jiamao.goplay.util.KeyInputUtils;
import com.tamsiree.rxkit.view.RxToast;

import me.goldze.mvvmhabit.bus.Messenger;

public class UpdateNameFragment  extends BaseMVVMFragment<UserUpdateNameLayoutBinding, UpdateInfoVM> {

   private String title="设置昵称";
    private int maxLen = 12 ;

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.user_update_name_layout;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }


    @Override
    public UpdateInfoVM initViewModel() {
        //使用自定义的ViewModelFactory来创建ViewModel，如果不重写该方法，则默认会调用LoginViewModel(@NonNull Application application)构造方法
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getActivity().getApplication());
        return ViewModelProviders.of(this, factory).get(UpdateInfoVM.class);
    }


    @Override
    public void initData() {
        super.initData();
        int type;

        Bundle mBundle = getArguments();
        if (mBundle != null) {
            type = mBundle.getInt("type");
            switch (type){
                case 1:
                    title = "设置昵称";
                    binding.inputName.setHint("请输入昵称");
                    break;
                case 2:
                    title = "关于我";
                    //设置高度
                    ViewGroup.LayoutParams layoutParams = binding.inputName.getLayoutParams();
                    layoutParams.height = DensityUtil.dip2px(getContext(),300);
                    binding.inputName.setLayoutParams(layoutParams);
                    //设置输入类型
                    binding.inputName.setGravity(Gravity.LEFT|Gravity.TOP);
                    maxLen = 150;
                    binding.inputName.setHint("请输入关于我的介绍");
                    break;
                case 3:
                    title = "添加学校";
                    binding.inputName.setHint("请输入学校");
                    maxLen = 20;
                    break;
                case 4:
                    title = "添加公司";
                    binding.inputName.setHint("请输入公司");
                    break;
            }
            binding.inputName.setText( mBundle.getString("name"));
            binding.inputName.setSelection( mBundle.getString("name").length());
        }
        binding.inputName.setFilters(new InputFilter[]{new InputFilter.LengthFilter(maxLen)});//20
        ImmersionBar.with(this). keyboardEnable(false).statusBarDarkFont(true).statusBarView(binding.getRoot().findViewById(R.id.status_bar_view)).init();
        TextView titleTv =  binding.getRoot().findViewById(R.id.tv_center_title);
        titleTv.setText(title);
        binding.getRoot().findViewById(R.id.tv_left_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getActivity().finish();
            }
        });

        binding.getRoot().findViewById(R.id.ll_menu).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (TextUtils.isEmpty(     binding.inputName.getText().toString())){
                    RxToast.error("数据不允许为空");
                    return;
                }
                getArguments().putString("name", binding.inputName.getText().toString());
                Messenger.getDefault().send(getArguments(),Config.MGER_USER_EDIT_NAME);
                getActivity().finish();


            }
        });

        viewModel.len.set( binding.inputName.getText().length()+"/"+maxLen);
        binding.inputName.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                viewModel.len.set(charSequence.length()+"/"+maxLen);
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });


        new Handler().postDelayed(new Runnable(){
            public void run() {
                binding.inputName.requestFocus();
                KeyInputUtils.showInputMethodWindow(binding.inputName);
            }
        }, 500);
    }
}
